package com.lihang;

import me.yugy.github.shadowlibrary.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.*;
import ohos.agp.utils.*;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;
import ohos.multimodalinput.event.TouchEvent;

import java.io.IOException;


/**
 * Created by leo
 * on 2019/7/9.
 * 阴影控件
 */
public class ShadowLayout extends StackLayout implements Component.DrawTask, Component.TouchEventListener {
    private Element clickAbleFalseDrawable;
    private int clickAbleFalseColor = -101;

    private Element layoutBackground;
    private Element layoutBackground_true;
    private Component firstView;

    private int mBackGroundColor;
    private int mBackGroundColor_true = -101;
    private int mShadowColor;
    private float mShadowLimit;
    private float mCornerRadius;
    private float mDx;
    private float mDy;
    private boolean leftShow;
    private boolean rightShow;
    private boolean topShow;
    private boolean bottomShow;
    private Paint shadowPaint;
    private Paint paint;

    private int leftPadding;
    private int topPadding;
    private int rightPadding;
    private int bottomPadding;
    //阴影布局子空间区域
    private RectFloat rectf = new RectFloat();

    //ShadowLayout的样式，是只需要pressed还是selected。默认是pressed.
    private int selectorType = 1;
    private boolean isShowShadow = true;
    private boolean isSym;

    //增加各个圆角的属性
    private float mCornerRadius_leftTop;
    private float mCornerRadius_rightTop;
    private float mCornerRadius_leftBottom;
    private float mCornerRadius_rightBottom;

    //边框画笔
    private Paint paint_stroke;
    private float stroke_with;
    private int stroke_color;
    private int stroke_color_true;

    private boolean isClickable;


    //关于控件填充渐变色
    private int startColor;
    private int centerColor;
    private int endColor;
    private int angle;


    //在普遍情况，在点击或选中按钮时，很可能伴随着textView的字体颜色变化
    private int mTextViewResId = -1;
    private Text mTextView;
    private int textColor;
    private int textColor_true;
    private String text;
    private String text_true;
    private boolean doSizeChanged = true;//标记是否已经执行过onSizeChanged方法，只执行一次
    private EventHandler handler;


    public ShadowLayout(Context context) {
        this(context, null);
    }

    public ShadowLayout(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }


    public ShadowLayout(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, String.valueOf(defStyleAttr));
        initView(context, attrs);
    }


    public void setClickable(boolean clickable) {
        super.setClickable(clickable);
        this.isClickable = clickable;
        changeSwitchClickable();
        if (isClickable) {
            super.setClickedListener(onClickListener);
        }

        if (paint != null) {
            if (startColor != -101 && endColor != -101) {
                gradient(paint);
            }
        }
    }


    //解决xml设置clickable = false时。代码设置true时，点击事件无效的bug
    private ClickedListener onClickListener;

    @Override
    public void setClickedListener(ClickedListener listener) {
        this.onClickListener = listener;
        if (isClickable) {
            super.setClickedListener(listener);
        }
    }

    public void changeSwitchClickable() {
        //不可点击的状态只在press mode的模式下生效
        if (selectorType == 1 && firstView != null) {

            //press mode
            if (!isClickable) {
                //不可点击的状态。
                if (clickAbleFalseColor != -101) {
                    //说明设置了颜色
                    if (layoutBackground != null) {
                        //说明此时是设置了图片的模式
                        firstView.getBackgroundElement().setAlpha(0);
                    }
                    paint.setColor(new Color(clickAbleFalseColor));
                    invalidate();

                } else if (clickAbleFalseDrawable != null) {
                    //说明设置了背景图
                    setmBackGround(clickAbleFalseDrawable);
                    paint.setColor(new Color(Color.getIntColor("#00000000")));
                    invalidate();
                }
            } else {
                //可点击的状态
                if (layoutBackground != null) {
                    setmBackGround(layoutBackground);
                } else {
                    if (firstView.getBackgroundElement() != null) {
                        firstView.getBackgroundElement().setAlpha(0);
                    }
                }
                paint.setColor(new Color(mBackGroundColor));
                invalidate();
            }
        }
    }

    //增加selector样式
    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (getWidth() != 0) {
            if (selectorType == 2) {
                if (selected) {
                    if (mBackGroundColor_true != -101) {
                        paint.setColor(new Color(mBackGroundColor_true));
                        invalidate();
                    }

                    paint.setShader(null, Paint.ShaderType.LINEAR_SHADER);
                    if (stroke_color_true != -101) {
                        paint_stroke.setColor(new Color(stroke_color_true));
                    }

                    if (layoutBackground_true != null) {
                        setmBackGround(layoutBackground_true);
                    }

                    if (mTextView != null) {
                        mTextView.setTextColor(new Color(textColor_true));
                        if (!TextTool.isNullOrEmpty(text_true)) {
                            mTextView.setText(text_true);
                        }
                    }

                } else {
                    paint.setColor(new Color(mBackGroundColor));
                    if (startColor != -101) {
                        gradient(paint);
                    }

                    if (stroke_color != -101) {
                        paint_stroke.setColor(new Color(stroke_color));
                    }
                    invalidate();

                    if (layoutBackground != null) {
                        setmBackGround(layoutBackground);
                    }

                    if (mTextView != null) {
                        mTextView.setTextColor(new Color(textColor));
                        if (!TextTool.isNullOrEmpty(text)) {
                            mTextView.setText(text);
                        }
                    }

                }
            }
        } else {
            setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
                @Override
                public void onRefreshed(Component component) {
                    setLayoutRefreshedListener(null);
                    setSelected(isSelected());
                }
            });
        }
    }

    //是否隐藏阴影
    public void setShadowHidden(boolean isShowShadow) {
        this.isShowShadow = !isShowShadow;
        if (getWidth() != 0 && getHeight() != 0) {
            setBackgroundCompat(getWidth(), getHeight());
        }
    }

    //设置x轴偏移量
    public void setShadowOffsetX(float mDx) {
        if (isShowShadow) {
            if (Math.abs(mDx) > mShadowLimit) {
                if (mDx > 0) {
                    this.mDx = mShadowLimit;
                } else {
                    this.mDx = -mShadowLimit;
                }
            } else {
                this.mDx = mDx;
            }
            setPadding();
            setBackgroundCompat(getWidth(), getHeight());
        }
    }

    //动态设置y轴偏移量
    public void setShadowOffsetY(float mDy) {
        if (isShowShadow) {
            if (Math.abs(mDy) > mShadowLimit) {
                if (mDy > 0) {
                    this.mDy = mShadowLimit;
                } else {
                    this.mDy = -mShadowLimit;
                }
            } else {
                this.mDy = mDy;
            }
            setPadding();
            setBackgroundCompat(getWidth(), getHeight());
        }
    }


    public float getCornerRadius() {
        return mCornerRadius;
    }

    //动态设置 圆角属性
    public void setCornerRadius(int mCornerRadius) {
        this.mCornerRadius = mCornerRadius;
        if (getWidth() != 0 && getHeight() != 0) {
            setBackgroundCompat(getWidth(), getHeight());
        }
    }

    public float getShadowLimit() {
        return mShadowLimit;
    }

    //动态设置阴影扩散区域
    public void setShadowLimit(int mShadowLimit) {
        if (isShowShadow) {
            int dip5 = dip2px(5);
            if (mShadowLimit >= dip5) {
                this.mShadowLimit = mShadowLimit;
            } else {
                this.mShadowLimit = dip5;
            }
            setPadding();
            setBackgroundCompat(getWidth(), getHeight());
        }
    }

    //动态设置阴影颜色值
    public void setShadowColor(int mShadowColor) {
        this.mShadowColor = mShadowColor;
        if (getWidth() != 0 && getHeight() != 0) {
            setBackgroundCompat(getWidth(), getHeight());
        }
    }


    public void setSpecialCorner(int leftTop, int rightTop, int leftBottom, int rightBottom) {
        mCornerRadius_leftTop = leftTop;
        mCornerRadius_rightTop = rightTop;
        mCornerRadius_leftBottom = leftBottom;
        mCornerRadius_rightBottom = rightBottom;
        if (getWidth() != 0 && getHeight() != 0) {
            setBackgroundCompat(getWidth(), getHeight());
        }
    }


    //是否隐藏阴影的上边部分
    public void setShadowHiddenTop(boolean topShow) {
        this.topShow = !topShow;
        resetPadding();
    }

    private void resetPadding() {
        setPadding();
        handler.postTask(() -> setBackgroundCompat(getWidth(), getHeight()), 15);
    }

    public void setShadowHiddenBottom(boolean bottomShow) {
        this.bottomShow = !bottomShow;
        resetPadding();
    }


    public void setShadowHiddenRight(boolean rightShow) {
        this.rightShow = !rightShow;
        resetPadding();
    }


    public void setShadowHiddenLeft(boolean leftShow) {
        this.leftShow = !leftShow;
        resetPadding();
    }


    public void setLayoutBackground(int color) {
        if (layoutBackground_true != null) {
            throw new UnsupportedOperationException("使用了ShadowLayout_hl_layoutBackground_true属性，要与ShadowLayout_hl_layoutBackground属性统一为颜色");
        }
        mBackGroundColor = color;
        if (selectorType == 2) {
            //select模式
            if (!this.isSelected()) {
                paint.setColor(new Color(mBackGroundColor));
            }
        } else {
            paint.setColor(new Color(mBackGroundColor));
        }
        invalidate();
    }


    public void setLayoutBackgroundTrue(int color) {
        if (layoutBackground != null) {
            throw new UnsupportedOperationException("使用了ShadowLayout_hl_layoutBackground属性，要与ShadowLayout_hl_layoutBackground_true属性统一为颜色");
        }
        mBackGroundColor_true = color;
        if (selectorType == 2) {
            //select模式
            if (this.isSelected()) {
                paint.setColor(new Color(mBackGroundColor_true));
            }
        }
        invalidate();
    }


    public void setStrokeColor(int color) {
        stroke_color = color;
        if (selectorType == 2) {
            //select模式
            if (!this.isSelected()) {
                paint_stroke.setColor(new Color(stroke_color));
            }
        } else {
            paint_stroke.setColor(new Color(stroke_color));
        }
        invalidate();
    }


    public void setStrokeColorTrue(int color) {

        stroke_color_true = color;
        if (selectorType == 2) {
            //select模式
            if (this.isSelected()) {
                paint_stroke.setColor(new Color(stroke_color_true));
            }
        }
        invalidate();
    }

    public void setStrokeWidth(int stokeWidth) {
        this.stroke_with = stokeWidth;
        if (stroke_with > dip2px(7)) {
            stroke_with = dip2px(5);
        }
        paint_stroke.setStrokeWidth(stroke_with);
        invalidate();
    }

    private void onFinishInflate() {
        if (mTextViewResId != -1) {
            mTextView = (Text) findComponentById(mTextViewResId);
            if (mTextView == null) {
                throw new NullPointerException("ShadowLayout找不到hl_bindTextView，请确保绑定的资源id在ShadowLayout内");
            } else {
                if (textColor == -101) {
                    textColor = mTextView.getTextColor().getValue();
                }


                if (textColor_true == -101) {
                    textColor_true = mTextView.getTextColor().getValue();
                }

                mTextView.setTextColor(new Color(textColor));
                if (!TextTool.isNullOrEmpty(text)) {
                    mTextView.setText(text);
                }
            }
        }

        firstView = getComponentAt(0);
        if (firstView instanceof Text) {
        }
        if (firstView == null) {
            firstView = ShadowLayout.this;
            //当子View都没有的时候。默认不使用阴影
            isShowShadow = false;
        }

        if (firstView != null) {
            //selector样式不受clickable的影响

            if (selectorType == 2) {
                //if (this.isSelected()) {
                //这个方法内已经判断了是否为空
                //  setmBackGround(layoutBackground_true);
                //} else {
                //  setmBackGround(layoutBackground);
                //  }
            } else {
                if (isClickable) {
                    setmBackGround(layoutBackground);
                } else {
                    setmBackGround(clickAbleFalseDrawable);
                    if (clickAbleFalseColor != -101) {
                        paint.setColor(new Color(clickAbleFalseColor));
                    }
                }
            }

        }
    }

    private void onSizeChanged(int w, int h) {
        if (w > 0 && h > 0) {
            if (startColor != -101) {
                gradient(paint);
            }
        }
        setBackgroundCompat(w, h);
    }


    @Override
    public void addComponent(Component childComponent) {
        super.addComponent(childComponent);
//        childComponent.addDrawTask(new ChildDrawTask());
        if (getChildIndex(childComponent) == 0) {
            onFinishInflate();
        }
    }

    class ChildDrawTask implements DrawTask {
        @Override
        public void onDraw(Component component, Canvas canvas) {
            dispatchDraw(canvas);
        }
    }

    private void initView(Context context, AttrSet attrs) {
        initAttributes(attrs);
        handler = new EventHandler(EventRunner.current());
        shadowPaint = new Paint();
        shadowPaint.setAntiAlias(true);
        shadowPaint.setStyle(Paint.Style.FILL_STYLE);


        paint_stroke = new Paint();
        paint_stroke.setAntiAlias(true);
        paint_stroke.setStyle(Paint.Style.STROKE_STYLE);
        paint_stroke.setStrokeWidth(stroke_with);
        if (stroke_color != -101) {
            paint_stroke.setColor(new Color(stroke_color));
        }


        //矩形画笔
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL_STYLE);
        //打个标记
        paint.setColor(new Color(mBackGroundColor));

        setPadding();

        addDrawTask(this, BETWEEN_BACKGROUND_AND_CONTENT);
        setTouchEventListener(this);
    }


    //将画笔附上 渐变色
    public void gradient(Paint paint) {
        if (!isClickable) {
            paint.setShader(null, Paint.ShaderType.LINEAR_SHADER);
            return;
        }

        //左上 x,y   leftPadding, topPadding,
        //右下 x,y   getWidth() - rightPadding, getHeight() - bottomPadding
        Color[] colors;
        if (centerColor == -101) {
            colors = new Color[]{new Color(startColor), new Color(endColor)};
        } else {
            colors = new Color[]{new Color(startColor), new Color(centerColor), new Color(endColor)};
        }

        if (angle < 0) {
            int trueAngle = angle % 360;
            angle = trueAngle + 360;
        }

        //当设置的角度大于0的时候
        //这个要算出每隔45度
        int trueAngle = angle % 360;
        int angleFlag = trueAngle / 45;
        Shader linearGradient;
        switch (angleFlag) {
            case 0://0°
                Point[] point0 = new Point[2];
                point0[0] = new Point(leftPadding, topPadding);
                point0[1] = new Point(getWidth() - rightPadding, topPadding);
                linearGradient = new LinearShader(point0, null, colors, Shader.TileMode.CLAMP_TILEMODE);
                paint.setShader(linearGradient, Paint.ShaderType.LINEAR_SHADER);
                break;
            case 1://45°
                Point[] point45 = new Point[2];
                point45[0] = new Point(leftPadding, getHeight() - bottomPadding);
                point45[1] = new Point(getWidth() - rightPadding, topPadding);
                linearGradient = new LinearShader(point45, null, colors, Shader.TileMode.CLAMP_TILEMODE);
                paint.setShader(linearGradient, Paint.ShaderType.LINEAR_SHADER);
                break;

            case 2://90°
                int x_ = (getWidth() - rightPadding - leftPadding) / 2 + leftPadding;
                Point[] point90 = new Point[2];
                point90[0] = new Point(x_, getHeight() - bottomPadding);
                point90[1] = new Point(x_, topPadding);
                linearGradient = new LinearShader(point90, null, colors, Shader.TileMode.CLAMP_TILEMODE);
                paint.setShader(linearGradient, Paint.ShaderType.LINEAR_SHADER);
                break;

            case 3://135°
                Point[] point135 = new Point[2];
                point135[0] = new Point(getWidth() - rightPadding, getHeight() - bottomPadding);
                point135[1] = new Point(leftPadding, topPadding);
                linearGradient = new LinearShader(point135, null, colors, Shader.TileMode.CLAMP_TILEMODE);
                paint.setShader(linearGradient, Paint.ShaderType.LINEAR_SHADER);
                break;
            case 4://180°
                Point[] point180 = new Point[2];
                point180[0] = new Point(getWidth() - rightPadding, topPadding);
                point180[1] = new Point(leftPadding, topPadding);
                linearGradient = new LinearShader(point180, null, colors, Shader.TileMode.CLAMP_TILEMODE);
                paint.setShader(linearGradient, Paint.ShaderType.LINEAR_SHADER);
                break;

            case 5://225°
                Point[] point225 = new Point[2];
                point225[0] = new Point(getWidth() - rightPadding, topPadding);
                point225[1] = new Point(leftPadding, getHeight() - bottomPadding);
                linearGradient = new LinearShader(point225, null, colors, Shader.TileMode.CLAMP_TILEMODE);
                paint.setShader(linearGradient, Paint.ShaderType.LINEAR_SHADER);
                break;

            case 6://270°
                int x = (getWidth() - rightPadding - leftPadding) / 2 + leftPadding;
                Point[] point270 = new Point[2];
                point270[0] = new Point(x, topPadding);
                point270[1] = new Point(x, getHeight() - bottomPadding);
                linearGradient = new LinearShader(point270, null, colors, Shader.TileMode.CLAMP_TILEMODE);
                paint.setShader(linearGradient, Paint.ShaderType.LINEAR_SHADER);
                break;

            case 7://315°
                Point[] point315 = new Point[2];
                point315[0] = new Point(leftPadding, topPadding);
                point315[1] = new Point(getWidth() - rightPadding, getHeight() - bottomPadding);
                linearGradient = new LinearShader(point315, null, colors, Shader.TileMode.CLAMP_TILEMODE);
                paint.setShader(linearGradient, Paint.ShaderType.LINEAR_SHADER);
                break;
        }
    }


    public int dip2px(float dipValue) {
        float scale = getContext().getResourceManager().getDeviceCapability().screenDensity;
        return (int) (dipValue * (scale / 160) + 0.5f);
    }

    public void setPadding() {
        if (isShowShadow && mShadowLimit > 0) {
            //控件区域是否对称，默认是对称。不对称的话，那么控件区域随着阴影区域走
            if (isSym) {
                int xPadding = (int) (mShadowLimit + Math.abs(mDx));
                int yPadding = (int) (mShadowLimit + Math.abs(mDy));

                if (leftShow) {
                    leftPadding = xPadding;
                } else {
                    leftPadding = 0;
                }

                if (topShow) {
                    topPadding = yPadding;
                } else {
                    topPadding = 0;
                }


                if (rightShow) {
                    rightPadding = xPadding;
                } else {
                    rightPadding = 0;
                }

                if (bottomShow) {
                    bottomPadding = yPadding;
                } else {
                    bottomPadding = 0;
                }
            } else {
                if (Math.abs(mDy) > mShadowLimit) {
                    if (mDy > 0) {
                        mDy = mShadowLimit;
                    } else {
                        mDy = 0 - mShadowLimit;
                    }
                }


                if (Math.abs(mDx) > mShadowLimit) {
                    if (mDx > 0) {
                        mDx = mShadowLimit;
                    } else {
                        mDx = 0 - mShadowLimit;
                    }
                }

                if (topShow) {
                    topPadding = (int) (mShadowLimit - mDy);
                } else {
                    topPadding = 0;
                }

                if (bottomShow) {
                    bottomPadding = (int) (mShadowLimit + mDy);
                } else {
                    bottomPadding = 0;
                }


                if (rightShow) {
                    rightPadding = (int) (mShadowLimit - mDx);
                } else {
                    rightPadding = 0;
                }


                if (leftShow) {
                    leftPadding = (int) (mShadowLimit + mDx);
                } else {
                    leftPadding = 0;
                }
            }
            setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
        }
    }

    @SuppressWarnings("deprecation")
    private void setBackgroundCompat(int w, int h) {
        if (isShowShadow) {
            //判断传入的颜色值是否有透明度
            isAddAlpha(mShadowColor);
            PixelMap bitmap = createShadowBitmap(w, h, mCornerRadius, mShadowLimit, mDx, mDy, mShadowColor, paint);
            PixelMapElement drawable = new PixelMapElement(bitmap);
            setBackground(drawable);
        } else {
            if (getComponentAt(0) == null) {
                if (layoutBackground != null) {
                    firstView = ShadowLayout.this;
                    if (isClickable) {
                        setmBackGround(layoutBackground);
                    } else {
                        changeSwitchClickable();
                    }
                } else {
                    //解决不执行onDraw方法的bug就是给其设置一个透明色
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(0x00000000));

                    this.setBackground(shapeElement);
                }
            } else {
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(RgbColor.fromArgbInt(0x00000000));
                this.setBackground(shapeElement);
            }


        }

    }


    private void initAttributes(AttrSet attrs) {
        if (attrs == null) {
            return;
        }

        //默认是显示
        isShowShadow = true;
        attrs.getAttr("hl_shadowHidden").ifPresent(attr -> isShowShadow = !attr.getBoolValue());
        leftShow = true;
        attrs.getAttr("hl_shadowHiddenLeft").ifPresent(attr -> leftShow = !attr.getBoolValue());
        rightShow = true;
        attrs.getAttr("hl_shadowHiddenRight").ifPresent(attr -> rightShow = !attr.getBoolValue());
        bottomShow = true;
        attrs.getAttr("hl_shadowHiddenBottom").ifPresent(attr -> bottomShow = !attr.getBoolValue());
        topShow = true;
        attrs.getAttr("hl_shadowHiddenTop").ifPresent(attr -> topShow = !attr.getBoolValue());
        mCornerRadius = 0;
        attrs.getAttr("hl_cornerRadius").ifPresent(attr -> mCornerRadius = attr.getDimensionValue());
        mCornerRadius_leftTop = -1;
        attrs.getAttr("hl_cornerRadius_leftTop").ifPresent(attr -> mCornerRadius_leftTop = attr.getDimensionValue());
        mCornerRadius_leftBottom = -1;
        attrs.getAttr("hl_cornerRadius_leftBottom").ifPresent(attr -> mCornerRadius_leftBottom = attr.getDimensionValue());
        mCornerRadius_rightTop = -1;
        attrs.getAttr("hl_cornerRadius_rightTop").ifPresent(attr -> mCornerRadius_rightTop = attr.getDimensionValue());
        mCornerRadius_rightBottom = -1;
        attrs.getAttr("hl_cornerRadius_rightBottom").ifPresent(attr -> mCornerRadius_rightBottom = attr.getDimensionValue());
        //默认扩散区域宽度
        mShadowLimit = 0;
        attrs.getAttr("hl_shadowLimit").ifPresent(attr -> mShadowLimit = attr.getDimensionValue());
        if (mShadowLimit == 0) {
            //如果阴影没有设置阴影扩散区域，那么默认隐藏阴影
            isShowShadow = false;
        } else {
            int dip5 = dip2px(5);
            if (mShadowLimit < dip5) {
                mShadowLimit = dip5;
            }
        }

        //x轴偏移量
        mDx = 0;
        attrs.getAttr("hl_shadowOffsetX").ifPresent(attr -> mDx = attr.getDimensionValue());
        //y轴偏移量
        mDy = 0;
        attrs.getAttr("hl_shadowOffsetY").ifPresent(attr -> mDy = attr.getDimensionValue());

        try {
            mShadowColor = getContext().getResourceManager().getElement(ResourceTable.Color_default_shadow_color).getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        attrs.getAttr("hl_shadowColor").ifPresent(attr -> mShadowColor = attr.getColorValue().getValue());
        selectorType = 1;
        attrs.getAttr("hl_shapeMode").ifPresent(attr -> {
            switch (attr.getStringValue()) {
                case "pressed":
                    selectorType = 1;
                    break;
                case "selected":
                    selectorType = 2;
                    break;
                case "ripple":
                    selectorType = 3;
                    break;
            }
        });
        isSym = true;
        attrs.getAttr("hl_shadowSymmetry").ifPresent(attr -> isSym = attr.getBoolValue());
        //背景颜色的点击(默认颜色为白色)
        try {
            mBackGroundColor = getContext().getResourceManager().getElement(ResourceTable.Color_default_shadowback_color).getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        Element background = null;
        if (attrs.getAttr("hl_layoutBackground").isPresent()) {
            background = attrs.getAttr("hl_layoutBackground").get().getElement();
        }
        if (background != null) {
            if (background instanceof PixelMapElement) {
                layoutBackground = background;
            } else {
                mBackGroundColor = attrs.getAttr("hl_layoutBackground").get().getColorValue().getValue();
            }
        }

        Element trueBackground = null;
        if (attrs.getAttr("hl_layoutBackground_true").isPresent()) {
            trueBackground = attrs.getAttr("hl_layoutBackground_true").get().getElement();
        }
        if (trueBackground != null) {
            if (trueBackground instanceof PixelMapElement) {
                layoutBackground_true = trueBackground;
            } else {
                mBackGroundColor_true = attrs.getAttr("hl_layoutBackground_true").get().getColorValue().getValue();
            }
        }

        if (mBackGroundColor_true != -101 && layoutBackground != null) {
            throw new UnsupportedOperationException("使用了ShadowLayout_hl_layoutBackground_true属性，必须先设置ShadowLayout_hl_layoutBackground属性。且设置颜色时，必须保持都为颜色");
        }

        if (layoutBackground == null && layoutBackground_true != null) {
            throw new UnsupportedOperationException("使用了ShadowLayout_hl_layoutBackground_true属性，必须先设置ShadowLayout_hl_layoutBackground属性。且设置图片时，必须保持都为图片");
        }

        //边框颜色的点击
        stroke_color = -101;
        attrs.getAttr("hl_strokeColor").ifPresent(attr -> stroke_color = attr.getColorValue().getValue());
        stroke_color_true = -101;
        attrs.getAttr("hl_strokeColor_true").ifPresent(attr -> stroke_color_true = attr.getColorValue().getValue());
        if (stroke_color == -101 && stroke_color_true != -101) {
            throw new UnsupportedOperationException("使用了ShadowLayout_hl_strokeColor_true属性，必须先设置ShadowLayout_hl_strokeColor属性");
        }

        stroke_with = dip2px(1);
        attrs.getAttr("hl_strokeWith").ifPresent(attr -> stroke_with = attr.getDimensionValue());
        //规定边框长度最大不错过7dp
        if (stroke_with > dip2px(7)) {
            stroke_with = dip2px(5);
        }


        Element clickAbleFalseBackground = null;
        if (attrs.getAttr("hl_layoutBackground_clickFalse").isPresent()) {
            clickAbleFalseBackground = attrs.getAttr("hl_layoutBackground_clickFalse").get().getElement();
        }
        if (clickAbleFalseBackground != null) {
            if (clickAbleFalseBackground instanceof PixelMapElement) {
                clickAbleFalseDrawable = clickAbleFalseBackground;
            } else {
                clickAbleFalseColor = attrs.getAttr("hl_layoutBackground_clickFalse").get().getColorValue().getValue();
            }
        }

        startColor = -101;
        attrs.getAttr("hl_startColor").ifPresent(attr -> startColor = attr.getColorValue().getValue());
        centerColor = -101;
        attrs.getAttr("hl_centerColor").ifPresent(attr -> centerColor = attr.getColorValue().getValue());
        endColor = -101;
        attrs.getAttr("hl_endColor").ifPresent(attr -> endColor = attr.getColorValue().getValue());
        if (startColor != -101) {
            //说明设置了渐变色的起始色
            if (endColor == -101) {
                throw new UnsupportedOperationException("使用了ShadowLayout_hl_startColor渐变起始色，必须搭配终止色ShadowLayout_hl_endColor");
            }
        }

        angle = 0;
        attrs.getAttr("hl_angle").ifPresent(attr -> angle = attr.getIntegerValue());
        if (angle % 45 != 0) {
            throw new IllegalArgumentException("Linear gradient requires 'angle' attribute to be a multiple of 45");
        }


        if (selectorType == 3) {
            //如果是ripple的话
            if (mBackGroundColor == -101 || mBackGroundColor_true == -101) {
                throw new NullPointerException("使用了ShadowLayout的水波纹，必须设置使用了ShadowLayout_hl_layoutBackground和使用了ShadowLayout_hl_layoutBackground_true属性，且为颜色值");
            }

            //如果是设置了图片的话，那么也不支持水波纹
            if (layoutBackground != null) {
                selectorType = 1;
            }
        }

        attrs.getAttr("hl_bindTextView").ifPresent(attr -> mTextViewResId = attr.getIntegerValue());
        textColor = -101;
        attrs.getAttr("hl_textColor").ifPresent(attr -> textColor = attr.getColorValue().getValue());
        textColor_true = -101;
        attrs.getAttr("hl_textColor_true").ifPresent(attr -> textColor_true = attr.getColorValue().getValue());
        text = "";
        attrs.getAttr("hl_text").ifPresent(attr -> text = attr.getStringValue());
        text_true = "";
        attrs.getAttr("hl_text_true").ifPresent(attr -> text_true = attr.getStringValue());

        isClickable = true;
        attrs.getAttr("clickable").ifPresent(attr -> isClickable = attr.getBoolValue());
        setClickable(isClickable);
    }


    private PixelMap createShadowBitmap(int shadowWidth, int shadowHeight, float cornerRadius, float shadowRadius,
                                        float dx, float dy, int shadowColor, Paint fillPaint) {
        //优化阴影bitmap大小,将尺寸缩小至原来的1/4。
        dx = dx / 4;
        dy = dy / 4;
        shadowWidth = shadowWidth / 4 == 0 ? 1 : shadowWidth / 4;
        shadowHeight = shadowHeight / 4 == 0 ? 1 : shadowHeight / 4;
        cornerRadius = cornerRadius / 4;
        shadowRadius = shadowRadius / 4;

        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.size = new Size(shadowWidth, shadowHeight);
        options.pixelFormat = PixelFormat.ARGB_8888;
        PixelMap output = PixelMap.create(options);
        Texture texture = new Texture(output);
        Canvas canvas = new Canvas(texture);

        //这里缩小limit的是因为，setShadowLayer后会将bitmap扩散到shadowWidth，shadowHeight
        //同时也要根据某边的隐藏情况去改变

        float rect_left = 0;
        float rect_right = 0;
        float rect_top = 0;
        float rect_bottom = 0;
        if (leftShow) {
            rect_left = shadowRadius;
        } else {
            rect_left = 0;
//            float maxLeftTop = Math.max(cornerRadius, mCornerRadius_leftTop);
//            float maxLeftBottom = Math.max(cornerRadius, mCornerRadius_leftBottom);
//            float maxLeft = Math.max(maxLeftTop, maxLeftBottom);
//            rect_left = maxLeft;
        }

        if (topShow) {
            rect_top = shadowRadius;
        } else {
            rect_top = 0;
//            float maxLeftTop = Math.max(cornerRadius, mCornerRadius_leftTop);
//            float maxRightTop = Math.max(cornerRadius, mCornerRadius_rightTop);
//            float maxTop = Math.max(maxLeftTop, maxRightTop);
//            rect_top = maxTop;
        }

        if (rightShow) {
            rect_right = shadowWidth - shadowRadius;
        } else {
            rect_right = shadowWidth;
//            float maxRightTop = Math.max(cornerRadius, mCornerRadius_rightTop);
//            float maxRightBottom = Math.max(cornerRadius, mCornerRadius_rightBottom);
//            float maxRight = Math.max(maxRightTop, maxRightBottom);
//            rect_right = shadowWidth - maxRight;
        }

        if (bottomShow) {
            rect_bottom = shadowHeight - shadowRadius;
        } else {
            rect_bottom = shadowHeight;
//            float maxLeftBottom = Math.max(cornerRadius, mCornerRadius_leftBottom);
//            float maxRightBottom = Math.max(cornerRadius, mCornerRadius_rightBottom);
//            float maxBottom = Math.max(maxLeftBottom, maxRightBottom);
//            rect_bottom = shadowHeight - maxBottom;
        }


        RectFloat shadowRect = new RectFloat(
                rect_left,
                rect_top,
                rect_right,
                rect_bottom);

        if (isSym) {
            if (dy > 0) {
                shadowRect.top += dy;
                shadowRect.bottom -= dy;
            } else if (dy < 0) {
                shadowRect.top += Math.abs(dy);
                shadowRect.bottom -= Math.abs(dy);
            }

            if (dx > 0) {
                shadowRect.left += dx;
                shadowRect.right -= dx;
            } else if (dx < 0) {

                shadowRect.left += Math.abs(dx);
                shadowRect.right -= Math.abs(dx);
            }
        } else {
            shadowRect.top -= dy;
            shadowRect.bottom -= dy;
            shadowRect.right -= dx;
            shadowRect.left -= dx;
        }

        Color fillColor = fillPaint.getColor();
        if (paint.getColor().getValue() == 0) {
            fillColor = Color.WHITE;
        }
        shadowPaint.setColor(fillColor);
        BlurDrawLooper blurDrawLooper = new BlurDrawLooper(shadowRadius / 2, dx, dy, new Color(shadowColor));
        shadowPaint.setBlurDrawLooper(blurDrawLooper);

        if (mCornerRadius_leftBottom == -1 && mCornerRadius_leftTop == -1 && mCornerRadius_rightTop == -1 && mCornerRadius_rightBottom == -1) {
            //如果没有设置整个属性，那么按原始去画
            canvas.drawRoundRect(shadowRect, cornerRadius, cornerRadius, shadowPaint);
        } else {
            //目前最佳的解决方案
            rectf.left = leftPadding;
            rectf.top = topPadding;
            rectf.right = getWidth() - rightPadding;
            rectf.bottom = getHeight() - bottomPadding;

            shadowPaint.setAntiAlias(true);
            int leftTop;
            if (mCornerRadius_leftTop == -1) {
                leftTop = (int) mCornerRadius / 4;
            } else {
                leftTop = (int) mCornerRadius_leftTop / 4;
            }
            int leftBottom;
            if (mCornerRadius_leftBottom == -1) {
                leftBottom = (int) mCornerRadius / 4;
            } else {
                leftBottom = (int) mCornerRadius_leftBottom / 4;
            }

            int rightTop;
            if (mCornerRadius_rightTop == -1) {
                rightTop = (int) mCornerRadius / 4;
            } else {
                rightTop = (int) mCornerRadius_rightTop / 4;
            }

            int rightBottom;
            if (mCornerRadius_rightBottom == -1) {
                rightBottom = (int) mCornerRadius / 4;
            } else {
                rightBottom = (int) mCornerRadius_rightBottom / 4;
            }

            float[] outerR = new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom};//左上，右上，右下，左下
            Path path = new Path();
            path.addRoundRect(shadowRect, outerR, Path.Direction.CLOCK_WISE);
            canvas.drawPath(path, shadowPaint);
        }

        return output;
    }

    protected void dispatchDraw(Canvas canvas) {
        int trueHeight = (int) (rectf.bottom - rectf.top);
        if (getComponentAt(0) != null) {
            if (mCornerRadius_leftTop == -1 && mCornerRadius_leftBottom == -1 && mCornerRadius_rightTop == -1 && mCornerRadius_rightBottom == -1) {
                //说明没有设置过任何特殊角、且是半圆。
                if (mCornerRadius > trueHeight / 2) {
                    Path path = new Path();
                    path.addRoundRect(rectf, trueHeight / 2, trueHeight / 2, Path.Direction.CLOCK_WISE);
                    canvas.clipPath(path, Canvas.ClipOp.INTERSECT);
                } else {
                    Path path = new Path();
                    path.addRoundRect(rectf, mCornerRadius, mCornerRadius, Path.Direction.CLOCK_WISE);
                    canvas.clipPath(path, Canvas.ClipOp.INTERSECT);
                }
            } else {
                float[] outerR = getCornerValue(trueHeight);
                Path path = new Path();
                RectFloat rectFloat = new RectFloat(leftPadding, topPadding, getWidth() - rightPadding, getHeight() - bottomPadding);
                path.addRoundRect(rectFloat, outerR, Path.Direction.CLOCK_WISE);
                canvas.clipPath(path, Canvas.ClipOp.INTERSECT);
            }
        }
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (doSizeChanged) {//onSizeChanged只执行一次
            doSizeChanged = false;
            onSizeChanged(getEstimatedWidth(), getEstimatedHeight());
        }
        rectf.left = leftPadding;
        rectf.top = topPadding;
        rectf.right = getWidth() - rightPadding;
        rectf.bottom = getHeight() - bottomPadding;
        int trueHeight = (int) (rectf.bottom - rectf.top);
        //如果都为0说明，没有设置特定角，那么按正常绘制
        if (getComponentAt(0) != null) {
            if (mCornerRadius_leftTop == -1 && mCornerRadius_leftBottom == -1 && mCornerRadius_rightTop == -1 && mCornerRadius_rightBottom == -1) {
                if (mCornerRadius > trueHeight / 2) {
                    if (selectorType != 3) {
                        if (layoutBackground == null && layoutBackground_true == null) {
                            //画圆角矩形
                            canvas.save();
                            canvas.drawRoundRect(rectf
                                    , trueHeight / 2, trueHeight / 2, paint);
                            //解决边框线太细时，四角的width偏大和其他边不同
                            if (stroke_color != -101) {
                                int the_height = (int) (rectf.bottom - rectf.top);
                                int the_height_stoke = (int) (rectf.bottom - stroke_with / 2 - rectf.top - stroke_with / 2);
                                int trueCorner = (int) (trueHeight * the_height_stoke / 2 / the_height);

                                RectFloat rectFStroke = new RectFloat(rectf.left + stroke_with / 2, rectf.top + stroke_with / 2, rectf.right - stroke_with / 2, rectf.bottom - stroke_with / 2);
                                canvas.drawRoundRect(rectFStroke, trueCorner, trueCorner, paint_stroke);
                            }
                            canvas.restore();
                        }
                    } else {
//                        float[] outerR = getCornerValue(trueHeight);
//                        ripple(outerR);
                    }

                } else {
                    if (selectorType != 3) {
                        if (layoutBackground == null && layoutBackground_true == null) {
                            //这里存在问题，如果paint设置了渐变，那么必须给paint再设置一次颜色才生效，
                            //目前没找到具体原因，暂时通过这种方式来处理
                            if (paint.getShader() != null) {
                                paint.setColor(Color.WHITE);
                            }
                            canvas.drawRoundRect(rectf, mCornerRadius, mCornerRadius, paint);
                            if (stroke_color != -101) {
                                int the_height = (int) (rectf.bottom - rectf.top);
                                int the_height_stoke = (int) (rectf.bottom - stroke_with / 2 - rectf.top - stroke_with / 2);
                                int trueCorner = (int) (mCornerRadius * the_height_stoke / the_height);
                                RectFloat rectFStroke = new RectFloat(rectf.left + stroke_with / 2, rectf.top + stroke_with / 2, rectf.right - stroke_with / 2, rectf.bottom - stroke_with / 2);
                                canvas.drawRoundRect(rectFStroke, trueCorner, trueCorner, paint_stroke);
                            }
                        }
                    } else {
//                        float[] outerR = getCornerValue(trueHeight);
//                        ripple(outerR);
                    }
                }
            } else {
                if (layoutBackground == null && layoutBackground_true == null) {
                    setSpaceCorner(canvas, trueHeight);
                }
            }
        }
    }

    public float[] getCornerValue(int trueHeight) {
        int leftTop;
        int rightTop;
        int rightBottom;
        int leftBottom;
        if (mCornerRadius_leftTop == -1) {
            leftTop = (int) mCornerRadius;
        } else {
            leftTop = (int) mCornerRadius_leftTop;
        }

        if (leftTop > trueHeight / 2) {
            leftTop = trueHeight / 2;
        }

        if (mCornerRadius_rightTop == -1) {
            rightTop = (int) mCornerRadius;
        } else {
            rightTop = (int) mCornerRadius_rightTop;
        }

        if (rightTop > trueHeight / 2) {
            rightTop = trueHeight / 2;
        }

        if (mCornerRadius_rightBottom == -1) {
            rightBottom = (int) mCornerRadius;
        } else {
            rightBottom = (int) mCornerRadius_rightBottom;
        }

        if (rightBottom > trueHeight / 2) {
            rightBottom = trueHeight / 2;
        }


        if (mCornerRadius_leftBottom == -1) {
            leftBottom = (int) mCornerRadius;
        } else {
            leftBottom = (int) mCornerRadius_leftBottom;
        }

        if (leftBottom > trueHeight / 2) {
            leftBottom = trueHeight / 2;
        }

        float[] outerR = new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom};//左上，右上，右下，左下
        return outerR;
    }


    //这里是为了解决stokeWith很大时，圆角盖不住底部四个角的bug(issues #86)
    public float[] getCornerValueOther(int trueHeight, int stokeWith) {
        trueHeight = trueHeight - stokeWith * 2;
        int leftTop;
        int rightTop;
        int rightBottom;
        int leftBottom;
        if (mCornerRadius_leftTop == -1) {
            leftTop = (int) mCornerRadius;
        } else {
            leftTop = (int) mCornerRadius_leftTop;
        }

        if (leftTop > trueHeight / 2) {
            leftTop = trueHeight / 2;
        }

        if (mCornerRadius_rightTop == -1) {
            rightTop = (int) mCornerRadius;
        } else {
            rightTop = (int) mCornerRadius_rightTop;
        }

        if (rightTop > trueHeight / 2) {
            rightTop = trueHeight / 2;
        }

        if (mCornerRadius_rightBottom == -1) {
            rightBottom = (int) mCornerRadius;
        } else {
            rightBottom = (int) mCornerRadius_rightBottom;
        }

        if (rightBottom > trueHeight / 2) {
            rightBottom = trueHeight / 2;
        }


        if (mCornerRadius_leftBottom == -1) {
            leftBottom = (int) mCornerRadius;
        } else {
            leftBottom = (int) mCornerRadius_leftBottom;
        }

        if (leftBottom > trueHeight / 2) {
            leftBottom = trueHeight / 2;
        }
        leftTop = leftTop - stokeWith;
        rightTop = rightTop - stokeWith;
        leftBottom = leftBottom - stokeWith;
        rightBottom = rightBottom - stokeWith;

        float[] outerR = new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom};//左上，右上，右下，左下
        return outerR;
    }


    //这是自定义四个角的方法。
    private void setSpaceCorner(Canvas canvas, int trueHeight) {

        float[] outerR = getCornerValue(trueHeight);
        if (stroke_color != -101) {
            if (selectorType != 3) {
                ShapeElement mDrawables = new ShapeElement();
//                Paint mDrawablesPaint = new Paint();
                mDrawables.setCornerRadiiArray(outerR);
                if (paint.getShader() != null) {
                    mDrawables.setShaderType(Paint.ShaderType.LINEAR_SHADER.value());
                } else {
                    mDrawables.setRgbColor(RgbColor.fromArgbInt(paint.getColor().getValue()));
                }
//                canvas.drawPaint(mDrawablesPaint);
                mDrawables.setBounds(leftPadding, topPadding, getWidth() - rightPadding, getHeight() - bottomPadding);
                mDrawables.drawToCanvas(canvas);

                float[] outerR_stroke = getCornerValueOther(trueHeight, (int) stroke_with);
                ShapeElement mDrawablesStroke = new ShapeElement();
                mDrawablesStroke.setShape(ShapeElement.RECTANGLE);
                mDrawablesStroke.setCornerRadiiArray(outerR_stroke);
                mDrawablesStroke.setStroke((int) stroke_with, RgbColor.fromArgbInt(paint_stroke.getColor().getValue()));
                mDrawablesStroke.setBounds((int) (leftPadding + stroke_with / 2), (int) (topPadding + stroke_with / 2), (int) (getWidth() - rightPadding - stroke_with / 2), (int) (getHeight() - bottomPadding - stroke_with / 2));


                mDrawablesStroke.drawToCanvas(canvas);
            } else {
//                ripple(outerR);
            }

        } else {
            if (selectorType != 3) {
                ShapeElement mDrawables = new ShapeElement();
                mDrawables.setShape(ShapeElement.RECTANGLE);
                mDrawables.setCornerRadiiArray(outerR);
                if (paint.getShader() != null) {
                    mDrawables.setShaderType(Paint.ShaderType.LINEAR_SHADER.value());
//                    mDrawables.getPaint().setShader(this.paint.getShader());
                } else {
                    mDrawables.setRgbColor(RgbColor.fromArgbInt(paint.getColor().getValue()));
//                    mDrawables.getPaint().setColor();
                }
//                canvas.drawPaint(mDrawablesPaint);
                mDrawables.setBounds(leftPadding, topPadding, getWidth() - rightPadding, getHeight() - bottomPadding);
                mDrawables.drawToCanvas(canvas);
            } else {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    ripple(outerR);
//                }
            }
        }

    }


//    private void ripple(float[] outRadius) {
//
//        int[][] stateList = new int[][]{
//                new int[]{android.R.attr.state_pressed},
//                new int[]{android.R.attr.state_focused},
//                new int[]{android.R.attr.state_activated},
//                new int[]{}
//        };
//
//        //深蓝
//        int normalColor = mBackGroundColor;
//        //玫瑰红
//        int pressedColor = mBackGroundColor_true;
//        int[] stateColorList = new int[]{
//                pressedColor,
//                pressedColor,
//                pressedColor,
//                normalColor
//        };
//        ColorStateList colorStateList = new ColorStateList(stateList, stateColorList);
//
//        RoundRectShape roundRectShape = new RoundRectShape(outRadius, null, null);
//        ShapeDrawable maskDrawable = new ShapeDrawable();
//        maskDrawable.setShape(roundRectShape);
//        maskDrawable.getPaint().setStyle(Paint.Style.FILL);
//        if (startColor != -101) {
//            //如果设置了渐变色的话
//            gradient(maskDrawable.getPaint());
//        } else {
//            maskDrawable.getPaint().setColor(normalColor);
//        }
//
//        ShapeDrawable contentDrawable = new ShapeDrawable();
//        contentDrawable.setShape(roundRectShape);
//        contentDrawable.getPaint().setStyle(Paint.Style.FILL);
//
//        if (startColor != -101) {
//            //如果设置了渐变色的话
//            gradient(contentDrawable.getPaint());
//        } else {
//            contentDrawable.getPaint().setColor(normalColor);
//        }
//
//        //contentDrawable实际是默认初始化时展示的；maskDrawable 控制了rippleDrawable的范围
//        RippleDrawable rippleDrawable = new RippleDrawable(colorStateList, contentDrawable, maskDrawable);
//        firstView.setBackground(rippleDrawable);
//    }


    public void isAddAlpha(int color) {
        //获取单签颜色值的透明度，如果没有设置透明度，默认加上#2a
        if (Color.alpha(color) == 255) {
            String red = Integer.toHexString(ColorUtil.red(color));
            String green = Integer.toHexString(ColorUtil.green(color));
            String blue = Integer.toHexString(ColorUtil.blue(color));

            if (red.length() == 1) {
                red = "0" + red;
            }

            if (green.length() == 1) {
                green = "0" + green;
            }

            if (blue.length() == 1) {
                blue = "0" + blue;
            }
            String endColor = "#2a" + red + green + blue;
            mShadowColor = convertToColorInt(endColor);
        }
    }


    public static int convertToColorInt(String argb)
            throws IllegalArgumentException {

        if (!argb.startsWith("#")) {
            argb = "#" + argb;
        }

        return Color.getIntColor(argb);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (selectorType == 3) {
            //如果是水波纹模式，那么不需要进行下面的渲染，采用系统ripper即可
            if (isClickable) {
                switch (touchEvent.getAction()) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        if (selectorType == 3) {
                            if (mTextView != null) {
                                mTextView.setTextColor(new Color(textColor_true));
                                if (!TextTool.isNullOrEmpty(text_true)) {
                                    mTextView.setText(text_true);
                                }
                            }
                        }
                        break;

                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        if (selectorType == 3) {
                            if (mTextView != null) {
                                mTextView.setTextColor(new Color(textColor));
                                if (!TextTool.isNullOrEmpty(text)) {
                                    mTextView.setText(text);
                                }
                            }
                        }
                        break;


                }
            }
            return true;
        }

        if (mBackGroundColor_true != -101 || stroke_color_true != -101 || layoutBackground_true != null) {
            if (isClickable) {
                switch (touchEvent.getAction()) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        if (selectorType == 1) {
                            if (mBackGroundColor_true != -101) {
                                paint.setColor(new Color(mBackGroundColor_true));
                                //打个标记
                                paint.setShader(null, Paint.ShaderType.LINEAR_SHADER);
                            }
                            if (stroke_color_true != -101) {
                                paint_stroke.setColor(new Color(stroke_color_true));
                            }

                            if (layoutBackground_true != null) {
                                setmBackGround(layoutBackground_true);
                            }
                            invalidate();

                            if (mTextView != null) {
                                mTextView.setTextColor(new Color(textColor_true));
                                if (!TextTool.isNullOrEmpty(text_true)) {
                                    mTextView.setText(text_true);
                                }
                            }
                        }
                        break;

                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        if (selectorType == 1) {
                            //打个标记
                            paint.setColor(new Color(mBackGroundColor));
                            if (startColor != -101) {
                                gradient(paint);
                            }
                            if (stroke_color != -101) {
                                paint_stroke.setColor(new Color(stroke_color));
                            }

                            if (layoutBackground != null) {
                                setmBackGround(layoutBackground);
                            }
                            invalidate();

                            if (mTextView != null) {
                                mTextView.setTextColor(new Color(textColor));
                                if (!TextTool.isNullOrEmpty(text)) {
                                    mTextView.setText(text);
                                }
                            }
                        }
                        break;
                }
            }
        }
        return true;
    }


    public void setmBackGround(Element drawable) {
        if (firstView != null && drawable != null) {
            if (mCornerRadius_leftTop == -1 && mCornerRadius_leftBottom == -1 && mCornerRadius_rightTop == -1 && mCornerRadius_rightBottom == -1) {
                GlideRoundUtils.setRoundCorner(firstView, drawable, mCornerRadius);
            } else {
                int leftTop;
                if (mCornerRadius_leftTop == -1) {
                    leftTop = (int) mCornerRadius;
                } else {
                    leftTop = (int) mCornerRadius_leftTop;
                }
                int leftBottom;
                if (mCornerRadius_leftBottom == -1) {
                    leftBottom = (int) mCornerRadius;
                } else {
                    leftBottom = (int) mCornerRadius_leftBottom;
                }

                int rightTop;
                if (mCornerRadius_rightTop == -1) {
                    rightTop = (int) mCornerRadius;
                } else {
                    rightTop = (int) mCornerRadius_rightTop;
                }

                int rightBottom;
                if (mCornerRadius_rightBottom == -1) {
                    rightBottom = (int) mCornerRadius;
                } else {
                    rightBottom = (int) mCornerRadius_rightBottom;
                }

                GlideRoundUtils.setCorners(firstView, drawable, leftTop, leftBottom, rightTop, rightBottom);
            }
        }
    }
}
